<?php

// $Id: gradeview.php, 2010/09/04 afterlastangel Exp $

/**
 * This page lists all the instances of studentcp in a particular course
 *
 * @author  Le Kien Truc <afterlastangel@gmail.com>
 * @version $Id: gradeview.php, 2010/09/04 $
 * @package mod/studentcp
 */
/// (Replace studentcp with the name of your module and remove this line)

require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
require_once(dirname(__FILE__) . '/locallib.php');

$id = optional_param('id', 0, PARAM_INT); // course_module ID, or
$a = optional_param('a', 0, PARAM_INT);  // studentcp instance ID
$studentid = optional_param('student', 0, PARAM_INT);

//mode: view single, report ...
$mode = optional_param('mode', 'report', PARAM_ALPHA);
//get subject id
$subjectid = optional_param('get_subject', 0, PARAM_INT);
$semester = optional_param('get_semester', 0, PARAM_INT);

if ($id) {
    if (!$cm = get_coursemodule_from_id('studentcp', $id)) {
        error('Course Module ID was incorrect');
    }

    if (!$course = get_record('course', 'id', $cm->course)) {
        error('Course is misconfigured');
    }

    if (!$studentcp = get_record('studentcp', 'id', $cm->instance)) {
        error('Course module is incorrect');
    }
} else if ($a) {
    if (!$studentcp = get_record('studentcp', 'id', $a)) {
        error('Course module is incorrect');
    }
    if (!$course = get_record('course', 'id', $studentcp->course)) {
        error('Course is misconfigured');
    }
    if (!$cm = get_coursemodule_from_instance('studentcp', $studentcp->id, $course->id)) {
        error('Course Module ID was incorrect');
    }
} else {
    error('You must specify a course_module ID or an instance ID');
}

require_login($course, true, $cm);
if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
    print_error('badcontext');
}

require_capability('mod/studentcp:view', $context);
$student = false;
if ($studentid) {
    if ($studentid == $USER->id or has_capability('mod/studentcp:viewallgrade', $context)) {
        if (!$student = get_record('user', 'id', $studentid)) {
            error("No such user in this course");
        }
    }
    if ($studentid != $USER->id && !has_capability('mod/studentcp:viewallgrade', $context)) {

        error(get_string("nothavepermission", "studentcp"));
    }
} else {
    $studentid = $USER->id;
}

add_to_log($course->id, "studentcp", "view", "view.php?id=$cm->id", "$studentcp->id");

/// Print the page header
$strstudentcps = get_string('modulenameplural', 'studentcp');
$strstudentcp = get_string('modulename', 'studentcp');

$navlinks = array();
$navlinks[] = array('name' => $strstudentcps, 'link' => "index.php?id=$course->id", 'type' => 'activity');
$navlinks[] = array('name' => format_string($studentcp->name), 'link' => '', 'type' => 'activityinstance');

$navigation = build_navigation($navlinks);

print_header_simple(format_string($studentcp->name), '', $navigation, '', '', true,
        update_module_button($cm->id, $course->id, $strstudentcp), navmenu($course, $cm));

/// Print the main part of the page
show_tabs($cm, $context, 'grade');
print_box_start();
echo '<div align="center">';
echo "<center><a href=\"$CFG->wwwroot/user/view.php?id={$studentid}\">" . fullname($student) . "</a></center>";
echo "<form method=\"post\" action=\"gradeview.php?id={$cm->id}\">";
$gradelist = get_records('studentcp_grade', 'userid', $studentid);
print_month_selector($gradelist, $semester);
print_subject_selector($gradelist, $semester, $subjectid);


echo '<input type="hidden" name="student" value="' . $studentid . '">';
echo '<input type="hidden" name="mode" value="' . $mode . '">';
echo '<input type="submit" name="ok" value="' . get_string('ok') . "\" />\n";

echo '</form>';
echo '</div>';
print_box_end();
//Start grade list;
//print_heading($USER->id);
print_box_start();


$totalgrade = 0;
$totalcredit = 0;
foreach ($gradelist as $grade) {
    if ($subjectid) {
        if ($subjectid == $grade->subjectid) {
            $return = print_grade_table($grade);
            $totalgrade += $return['average'] * $return['credit'];
            $totalcredit += $return['credit'];
        }
    } else if ($semester) {
        if ($semester == $grade->semester) {
            $return = print_grade_table($grade);
            $totalgrade += $return['average'] * $return['credit'];
            $totalcredit += $return['credit'];
        }
    } else {
        $return = print_grade_table($grade);
        $totalgrade += $return['average'] * $return['credit'];
        $totalcredit += $return['credit'];
    }
}
if ($totalcredit) {
    print_heading(get_string('overall','studentcp'));
    echo  "<center><b>".$totalgrade / $totalcredit . "</b></center>";
}
print_box_end();

print_footer($course);
/// Finish the page
?>
